﻿using Dapper;
using Domain.Entity.System_dictionary;
using Domain.IRepository.System_dictionary;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;

namespace EFCore.Repository.System_dictionary
{
    [Dependency(ServiceLifetime.Scoped)]
    public class ChildrenRepoitory : IChildrenRepoitory
    {
        private readonly MyDbContext myDbContext;

        public ChildrenRepoitory(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        public List<Children> childrens()
        {
            string sql = "select * from [dbo].[Sys_dict_data] where [Dict_type]='One_address' ";
            List<Sys_dict_data> list = myDbContext.Database.GetDbConnection().Query<Sys_dict_data>(sql).ToList();
            List<Children> childrens = new List<Children>();
            if (list != null)
            {
                foreach (var item in list)
                {
                    Children children = new Children()
                    {
                        value = item.Dict_label,
                        label = item.Dict_label,
                        id = Convert.ToInt32(item.Dict_value)
                    };
                    string sql2 = $"select * from [dbo].[Sys_dict_data] where Dict_type='Two_address' and Dict_value='{children.id}' ";
                    List<Sys_dict_data> list2 = myDbContext.Database.GetDbConnection().Query<Sys_dict_data>(sql2).ToList();
                    List<Children> children2 = new List<Children>();
                    foreach (var item2 in list2)
                    {
                        Children children1 = new Children()
                        {
                            value = item2.Dict_label,
                            label = item2.Dict_label,
                            id = Convert.ToInt32(item2.Dict_value)
                        };
                        
                        string sql3 = $"select * from [dbo].[Sys_dict_data] where Dict_type='Three_address' and Dict_value='{children.id}'";
                        List<Sys_dict_data> list3 = myDbContext.Database.GetDbConnection().Query<Sys_dict_data>(sql3).ToList();
                        List<Children> childrens3 = new List<Children>();
                        foreach (var item3 in list3)
                        {
                            Children children4 = new Children()
                            {
                                value = item3.Dict_label,
                                label = item3.Dict_label,
                                id = Convert.ToInt32(item2.Dict_value)
                            };
                            childrens3.Add(children4);
                        }
                        children1.children = childrens3;
                        children2.Add(children1);
                        children.children = children2;
                        childrens.Add(children);
                    }   
                    }
                }
                return childrens;
            }
        }
    }

